﻿Public Class FrmMotoLigne
    Public pDate As Date
    Public pCD_CLI As Integer
    Public pDispo As Integer
    Public pDispoRoute As Integer

    Private szSQL As String
    Private rs As ADODB.Recordset
    Dim oldCD_CLI As Integer

    Private Sub FrmMotoLigne_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.Text = LibelleMoto(gTypePlanning) & " du " & pDate & " - " & gGroupe & " - " & pCD_CLI
        ' planning moto ?
        If gTypePlanning = "A" Then
            chkROUTE.Visible = True
            BO_DISPO.Visible = True
        Else
            chkROUTE.Visible = False
            BO_DISPO.Visible = False
        End If
        ' utilisateur secretaire, autoriser suppression
        If gUserAcces < 6 Then
            BO_SUPPRIMER.Visible = False
        Else
            BO_SUPPRIMER.Visible = True
        End If

        InitComboStatutsLecon(CB_STATUT)
        InitComboMoniteur(CB_MONITEUR)
        InitComboLieux(CB_LIEUX, gTypePlanning & gGroupe)
        InscritToScreen()

    End Sub

    Private Sub InscritToScreen()
        Dim rsEleve As ADODB.Recordset = Nothing

        If pCD_CLI = 0 Then
            ' creation 
            ClearTextBoxes(Me)
            TE_DATE.Value = pDate
            TE_QTE.Clear()
            BO_ENREGISTRER.Enabled = True
            BO_RECHERCHER.Enabled = True
            BO_DISPO.Visible = True
        Else
            ' Lire le RDV sélectionné
            szSQL = "CALL spGet_PLA_MOTO_LIGNES('" & gTypePlanning & "','" & Format(pDate, "yyyy/MM/dd") & "','" & gGroupe & "'," & pCD_CLI & ")"
            GetRecordset(ctDSN_AEM, szSQL, rs)
            If rs.RecordCount > 0 Then
                TE_DATE.Value = rs.Fields("date").Value
                TE_CD_CLI.Text = rs.Fields("cd_cli").Value
                oldCD_CLI = TE_CD_CLI.Text
                TE_NOM.Text = nv(rs.Fields("nom").Value)
                ' Lire la fiche Eleves
                szSQL = "CALL spGet_ELEVES_byCD_CLI(" & nv(rs.Fields("cd_cli").Value) & ")"
                GetRecordset(ctDSN_AEM, szSQL, rsEleve)
                If rsEleve.RecordCount > 0 Then
                    TE_FORMULE.Text = rsEleve.Fields("formule").Value
                Else
                    TE_FORMULE.Text = " "
                End If
                CB_LIEUX.SelectedIndex = CB_LIEUX.FindString(nv(rs.Fields("lieu").Value))
                CB_MONITEUR.SelectedIndex = CB_MONITEUR.FindString(nv(rs.Fields("cd_mon").Value.ToString))
                CB_STATUT.SelectedIndex = CB_STATUT.FindString(rs.Fields("statut").Value.ToString)
                TE_OBS.Text = nv(rs.Fields("comment").Value)
                TE_QTE.Text = rs.Fields("qte").Value
                CB_TARIFS.SelectedIndex = CB_TARIFS.FindString(nv(rs.Fields("ref").Value.ToString))
                chkROUTE.Checked = rs.Fields("route").Value
                ' incremente dispo en cas de changement plateau / route
                If chkROUTE.Checked = True Then
                    pDispoRoute += 1
                Else
                    pDispo += 1
                End If
                TE_MODIFIE_LE.Text = setModifiedBy(rs.Fields("cree_le").Value, rs.Fields("modif_le").Value, rs.Fields("cd_uti").Value)
                If nv(rs.Fields("valide").Value) = "O" Or gUserAcces < 2 Then
                    BO_ENREGISTRER.Enabled = False
                    BO_SUPPRIMER.Enabled = False
                    BO_RECHERCHER.Enabled = False
                    BO_DISPO.Visible = False
                Else
                    BO_ENREGISTRER.Enabled = True
                    BO_SUPPRIMER.Enabled = True
                    BO_RECHERCHER.Enabled = True
                    BO_DISPO.Visible = True
                End If
                rsEleve.Close()
                ' initialiser les tarifs selon la filière de l'élève
                InitComboTarifs(CB_TARIFS, TE_CD_CLI.Text)
            End If
            rs.Close()
        End If

    End Sub

    Private Function IsEleveOK() As Boolean

        IsEleveOK = False
        If TE_CD_CLI.TextLength = 0 Then
            Exit Function
        End If

        ' place dispo ?
        If chkROUTE.Checked = True Then
            If pDispoRoute < 1 Then
                MsgBox("Il n'y a plus de place Route disponible !")
                Exit Function
            End If
        Else
            If pDispo < 1 Then
                MsgBox("Il n'y a plus de place Plateau disponible !")
                Exit Function
            End If
        End If
        ' effectuer les controles sur l'élève
        If ControlerEleves("PLM", TE_CD_CLI.Text, gTypePlanning) = False Then
            Exit Function
        End If
        IsEleveOK = True

    End Function

    Private Sub BO_SUPPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_SUPPRIMER.Click

        gCD_CLI = TE_CD_CLI.Text
        ' utilisateur non manager ?
        If gUserAcces < 9 And IsEleve(gCD_CLI) Then
            ' controler le délai annulation ?
            If IsDelaiOK(pDate) = False Then
                Exit Sub
            End If
        End If

        ' suppression
        deleteEnregistrement("pla_moto_lignes", Me.Text, gTypePlanning, Format(pDate, "yyyy/MM/dd"), gGroupe, gCD_CLI)
        ' fermer la fenetre
        Me.Close()

    End Sub

    Private Sub BO_RECHERCHER_Click(sender As System.Object, e As System.EventArgs) Handles BO_RECHERCHER.Click
        Dim szHeureConduite As String = ""
        Dim nQte As Integer = 0

        ' si Elève non provisoire, proposer le dernier code client consulté
        If TE_CD_CLI.TextLength Then
            gCD_CLI = TE_CD_CLI.Text
        End If
        ' ouvre la fenetre de Recherche ELEVES
        frmElevesSearch.ShowDialog()

        ' ancien cd_cli est de type élève, le nouveau doit être également élève
        If oldCD_CLI > 100000 And gCD_CLI < 100000 Then
            MsgBox("Vous devenez sélectionner un élève !", MsgBoxStyle.Critical)
            Exit Sub
        End If
        TE_CD_CLI.Text = gCD_CLI
        TE_NOM.Text = frmElevesSearch.Nompren
        TE_FORMULE.Text = frmElevesSearch.Formule

        ' Lire la FORMULE sélectionnée
        szSQL = "CALL spGet_FORMULES('" & TE_FORMULE.Text & "')"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount > 0 Then
            szHeureConduite = Trim(rs.Fields("hconduit").Value)
            nQte = rs.Fields("hseance").Value
        End If
        rs.Close()

        ' Lire la fiche Elève
        szSQL = "CALL spGet_ELEVES_byCD_CLI(" & TE_CD_CLI.Text & ")"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount < 1 Then
            MsgBox("Elève " & TE_CD_CLI.Text & " non trouvé !", MsgBoxStyle.Critical)
            rs.Close()
            Exit Sub
        End If

        If IsDate(rs.Fields("tr_p_OK").Value) Then
            chkROUTE.Checked = True
        Else
            chkROUTE.Checked = False
        End If

        ' initialiser les tarifs selon la filière de l'élève
        InitComboTarifs(CB_TARIFS, TE_CD_CLI.Text)

        Select Case gTypePlanning
            Case "I"
                CB_TARIFS.SelectedIndex = CB_TARIFS.FindString("CIRCUIT")
                nQte = 1
            Case "R"
                CB_TARIFS.SelectedIndex = CB_TARIFS.FindString("RDVP")
            Case "D"
                CB_TARIFS.SelectedIndex = CB_TARIFS.FindString("PREPACODE")
                nQte = 1
            Case "P"
                CB_TARIFS.SelectedIndex = CB_TARIFS.FindString("PREPAPSY")
            Case "E"
                CB_TARIFS.SelectedIndex = CB_TARIFS.FindString("TB")
                nQte = 1
            Case Else
                CB_TARIFS.SelectedIndex = CB_TARIFS.FindString(szHeureConduite)
        End Select
        If TE_QTE.TextLength = 0 Then
            TE_QTE.Text = nQte
        End If

        rs.Close()

        ' si non eleve, interdire bouton "Fiche Eleve"
        If IsEleve(gCD_CLI) = False Then
            BO_ELEVE.Visible() = False
        Else
            BO_ELEVE.Visible() = True
        End If
        ' passe au champs suivant
        TE_OBS.Focus()

    End Sub

    Private Sub BO_ENREGISTRER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTRER.Click

        ' controler la saisie de l'élève
        If IsEleveOK() = False Then
            TE_CD_CLI.Focus()
            Exit Sub
        End If

        ' controler le solde réel de l'élève
        If ControlerSolde("REEL", TE_CD_CLI.Text) = False Then
            TE_CD_CLI.Focus()
            Exit Sub
        End If

        ' mise à jour 
        Dim szLieu As String = ""
        If CB_LIEUX.Items.Count > 0 Then
            szLieu = CB_LIEUX.SelectedItem.ToString
        End If
        szSQL = "CALL spUpd_PLA_MOTO_LIG('" & gTypePlanning & "','" & Format(pDate, "yyyy/MM/dd") & "','" & gGroupe & "'," & pCD_CLI & "," & _
             TE_CD_CLI.Text & ",'" & nq(TE_NOM.Text) & "','" & szLieu & "','" & getCodeFromCombo(CB_MONITEUR.SelectedItem) & "'," & _
             getCodeFromCombo(CB_STATUT.SelectedItem) & ",'" & TE_OBS.Text & "'," & TE_QTE.Text & ",'" & getCodeFromCombo(CB_TARIFS.SelectedItem) & "'," & _
             CInt(chkROUTE.Checked) & "," & gUserAgence & ",'" & gUserCode & "')"
        If ExecQuery(ctDSN_AEM, szSQL) Then
            gCD_CLI = TE_CD_CLI.Text
            ' mettre le rdv dans le compte Eleve 
            UpdateHeureMoto(gTypePlanning, pDate, gGroupe, getCodeFromCombo(CB_STATUT.SelectedItem), "N")
            Me.Close()
        End If


    End Sub

    Private Sub BO_ELEVE_Click(sender As System.Object, e As System.EventArgs) Handles BO_ELEVE.Click
        ' affiche la fiche de l'éleve
        If TE_CD_CLI.TextLength Then
            gCD_CLI = TE_CD_CLI.Text
            frmElevesMAJ.ShowDialog()
        End If
    End Sub

    Private Sub BO_DISPO_Click(sender As System.Object, e As System.EventArgs) Handles BO_DISPO.Click
        Dim szHeureConduite As String = ""
        Dim nQte As Integer = 0

        ' ouvre la fenetre de Recherche ELEVES disponible
        frmElevesDispo.pPlanning = "A"
        frmElevesDispo.pMoniteur = gGroupe
        frmElevesDispo.pDateHeure = Format(pDate, "yyyy/MM/dd")
        frmElevesDispo.ShowDialog()
        TE_CD_CLI.Text = gCD_CLI
        TE_NOM.Text = frmElevesDispo.pNompren
        TE_FORMULE.Text = frmElevesDispo.pFormule

        ' Lire la FORMULE sélectionnée
        szSQL = "CALL spGet_FORMULES('" & TE_FORMULE.Text & "')"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount > 0 Then
            szHeureConduite = Trim(rs.Fields("hconduit").Value)
            nQte = rs.Fields("hseance").Value
        End If
        rs.Close()

        ' Lire la fiche Elève
        szSQL = "CALL spGet_ELEVES_byCD_CLI(" & TE_CD_CLI.Text & ")"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount < 1 Then
            MsgBox("Elève " & TE_CD_CLI.Text & " non trouvé !", MsgBoxStyle.Critical)
            rs.Close()
            Exit Sub
        End If

        If IsDate(rs.Fields("tr_p_OK").Value) Then
            chkROUTE.Checked = True
        Else
            chkROUTE.Checked = False
        End If

        ' initialiser les tarifs selon la filière de l'élève
        InitComboTarifs(CB_TARIFS, TE_CD_CLI.Text)
        CB_TARIFS.SelectedIndex = CB_TARIFS.FindString(szHeureConduite)
        If TE_QTE.TextLength = 0 Then
            TE_QTE.Text = nQte
        End If

        rs.Close()

        BO_ELEVE.Visible() = True
        ' passe au champs suivant
        TE_OBS.Focus()

    End Sub
End Class